package com.chain.chl.ui.viewholder;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import butterknife.BindView;
import butterknife.ButterKnife;

import com.chain.chl.R;
import com.chain.chl.manager.CardManager;
import com.chain.chl.model.AddFavoriteModel;
import com.chain.chl.model.BaseResponse;
import com.chain.chl.model.card.Card;
import com.chain.chl.model.card.TxtImgCard;
import com.chain.chl.record.FavouriteShare;
import com.chain.chl.ui.activity.ImageMainActivity;
import com.chain.chl.ui.activity.TxtImgDetailActivity;
import com.chain.chl.ui.activity.WebActivity;
import com.chain.chl.util.FrescoLoadUtils;
import com.facebook.drawee.view.SimpleDraweeView;
import com.naschain.naspay.NasPayManager;
import com.nostra13.universalimageloader.utils.L;

import rx.Observer;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class TxtImgViewHolder extends RecyclerView.ViewHolder {

    @BindView(R.id.rootView)
    View rootView;
    @BindView(R.id.title)
    TextView textView;
    @BindView(R.id.simpleDraweeView)
    SimpleDraweeView simpleDraweeView;

    private Context mContext;

    public static TxtImgViewHolder createViewHolder(ViewGroup parentView) {
        View view = LayoutInflater.from(parentView.getContext())
                .inflate(R.layout.viewholder_rec_txtimg, parentView, false);
        return new TxtImgViewHolder(view);
    }

    public TxtImgViewHolder(View itemView) {
        super(itemView);
        mContext = itemView.getContext();
        ButterKnife.bind(this, itemView);
    }

    AlertDialog alertDialog;

    public void setData(final Card card) {
        textView.setText(card.getTitle());
        FrescoLoadUtils.Builder.newBuilderWithDraweeView(simpleDraweeView)
                .setUriStr(card.getCover())
                .build();
        rootView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (card.getHasPayed() == 1) {
                    WebActivity.open(mContext, card);
                } else {
                    if(FavouriteShare.hasAlreadyPayRecord(mContext,card.getUrl())){
                        WebActivity.open(mContext, card);
                    }else {
                        if (FavouriteShare.hasPendingPayRecord(mContext, card.getUrl())) {
                            Toast.makeText(mContext, "正在处理中，请稍后", Toast.LENGTH_SHORT).show();
                        } else {
                            new AlertDialog.Builder(mContext).setTitle("请使用0.0000001Nas购买后可以观看")
                                    .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                                        @Override
                                        public void onClick(DialogInterface dialog, int which) {
//                                            addFavouriteCard(card);
                                            FavouriteShare.addPendingPayRecord(mContext,card.getUnicode(),card);
                                            new NasPayManager.PayBuilder(mContext)
                                                    .setUnicode(card.getUnicode()).build();
                                            dialog.dismiss();
                                        }
                                    })
                                    .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                                        @Override
                                        public void onClick(DialogInterface dialog, int which) {
                                            dialog.dismiss();
                                        }
                                    })
                                    .create()
                                    .show();
                        }
                    }
                }
            }
        });
//        rootView.setOnLongClickListener(new View.OnLongClickListener() {
//            @Override public boolean onLongClick(View v) {
//                if (alertDialog == null) {
//                    alertDialog = new AlertDialog.Builder(itemView.getContext()).setTitle("是否取消收藏")
//                f        .setNegativeButton("取消", new DialogInterface.OnClickListener() {
//                            @Override public void onClick(DialogInterface dialog, int which) {
//                                alertDialog.dismiss();
//                            }
//                        })
//                        .setNegativeButton("确认", new DialogInterface.OnClickListener() {
//                            @Override public void onClick(DialogInterface dialog, int which) {
//                                CardManager.getInstance()
//                                    .removeFavouriteCard(card.getCardId())
//                                    .subscribeOn(Schedulers.io())
//                                    .observeOn(AndroidSchedulers.mainThread())
//                                    .subscribe(new Observer<BaseResponse>() {
//                                        @Override public void onCompleted() {
//
//                                        }
//
//                                        @Override public void onError(Throwable e) {
//
//                                        }
//
//                                        @Override
//                                        public void onNext(BaseResponse listBaseResponse) {
//
//                                        }
//                                    });
//                                if (onTxtImgViewHolderListener != null) {
//                                    onTxtImgViewHolderListener.deleteItem();
//                                }
//                                alertDialog.dismiss();
//                            }
//                        })
//                        .create();
//                }
//                alertDialog.show();
//                return true;
//            }
//        });
    }

    private OnTxtImgViewHolderListener onTxtImgViewHolderListener;

    public void setOnTxtImgViewHolderListener(
            OnTxtImgViewHolderListener onTxtImgViewHolderListener) {
        this.onTxtImgViewHolderListener = onTxtImgViewHolderListener;
    }

    public interface OnTxtImgViewHolderListener {
        void deleteItem();
    }
}
